import rclpy
from rclpy.node import Node
from std_msgs.msg import String

class SubscriberNode(Node):
    def __init__(self, name):
        super().__init__(name)
        self.sub = self.create_subscription(String, 'topic', self.listener_callback, 10) # 创建订阅者 （消息类型 话题名称 回调函数 队列大小
    
    def listener_callback(self, msg):
        self.logger().info("I heard %s" % msg.data)

def main(args=None):
    rclpy.init(args=args)
    node = SubscriberNode('Hello Ros2_sub')
    rclpy.spin(node) # 保持节点运行
    node.destroy_node()
    rclpy.shutdonw()
